// pages/malls/knowledge-list/knowledge-list.js
const app = getApp()
const constant = require("../../../constant.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeModule: 0,
    currentModuleId: 0,
    hideBottom: true,
    hideNoDataBottom: true,
    refreshTime: '', //刷新时间
    allPages: '', //总页数
    currentPage: 1, //当前页数，默认是1
    everyPage: 8,
    loadMoreData: '加载更多...'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var date = new Date();
    this.setData({
      qiniu: constant.resource.qiniu,
      refreshTime: date.toLocaleTimeString()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    wx.request({
      url: constant.api(app.globalData.pord).url + '/modules',
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            modules: res.data.data.modules
          });
        }
      },
      fail: function (res) { }
    })

    var moduleId = that.data.activeModule == 0 ? '' : that.data.activeModule;
    that.setData({
      currentModuleId: moduleId
    });
    that.articleData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  moduleClick: function(event) {
    var moduleId = event.currentTarget.dataset.id;
    this.setData({
      currentModuleId: moduleId
    });
    this.articleData();
  },

  articleData: function() {
    var that = this;
    var moduleId = that.data.currentModuleId;

    var moduleParam = '&m=' + moduleId;
    var url = constant.api(app.globalData.pord).url + '/articles?page=' + that.data.currentPage + '&per_page=' + that.data.everyPage;
    if(moduleId) {
      url = url + moduleParam;
    }

    wx.request({
      url: url,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          var moduleId = !that.data.currentModuleId ? 0 : that.data.currentModuleId;
          var currentArticles = res.data.data.articles;
          var originalDatas = that.data.articles;
          if (originalDatas && moduleId == that.data.activeModule) {
            originalDatas = originalDatas.concat(currentArticles);
          }
          that.setData({
            articles: (!originalDatas || moduleId != that.data.activeModule) ? currentArticles : originalDatas,
            currentPage: res.data.data.page.current_page,
            allPages: res.data.data.page.total_page,
            hideBottom: true
          });

          var moduleId = !res.data.data.m ? 0 : res.data.data.m;
            that.setData({
              activeModule: moduleId
            })
          
        }
      },
      fail: function (res) { }
    })
  },

  // 上拉加载更多
  loadMore: function () {
    var that = this;
    // 当前页是最后一页
    if (that.data.currentPage >= that.data.allPages) {
      that.setData({
        loadNoMessage: '无更多数据',
        hideNoDataBottom: false
      })
      return;
    }

    console.log('上拉加载更多');
    var tempCurrentPage = that.data.currentPage;
    tempCurrentPage = tempCurrentPage + 1;
    that.setData({
      currentPage: tempCurrentPage,
      hideBottom: false
    })

    setTimeout(function () {
      that.articleData();
    }, 1000);
  }
})